package leetcode.周赛.双周赛.第67场;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/12/11 23:59
 * @description：
 */
public class 适合打劫银行的日子 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(solution.goodDaysToRobBank(new int[]{77,9,111,138,139,183,112,127,38,123,198,86,163,50,140,106,99,140,152,176,124,181,14,113,53,186,76,165,32,26,137,4,186,193,130,157,173,52,27,101,154,129,34,200,51,184,127,147,197,151,190,95,62,182,77,34,174,162,7,84,105,103,128}, 2));
    }


    class Solution {
        public List<Integer> goodDaysToRobBank(int[] security, int time) {
            List<Integer> list = new ArrayList<>();
            if (time == 0) {
                for (int i = 0; i < security.length; i++) {
                    list.add(i);
                }
                return list;
            }
            int i = 0;
            int j = 0;
            int n = security.length;
            int preCount = 0;
            int tailCount = 0;
            while (j < n && i < n) {
                while ((i + 1) < n) {
                    if (security[i] < security[i + 1] || preCount == time) break;
                    i++;
                    preCount++;
                }
                if (preCount < time) {
                    preCount = 0;
                    i++;
                    continue;
                }
                j = i;
                while ((j + 1) < n) {
                    if (security[j] > security[j + 1] || tailCount == time) break;
                    j++;
                    tailCount++;
                }
                if (tailCount < time) {
                    tailCount = 0;
                    preCount--;
                    continue;
                }
                list.add(i);
                preCount--;
                tailCount = 0;
            }
            return list;
        }
    }
}
